﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace QuanLyThietBi
{
    class CXuly_loaivattu
    {
        SqlConnection con;
        public CXuly_loaivattu(SqlConnection connection)
        {
            con = connection;
            
        }
        public bool them(CLoaivattu a)
        {
            if (tim(a.Maloai) != null) return false;
            con.Open();
            string str = "insert into loaivattu values('" + a.Maloai + "',N'" + a.Tenloai + "')";
            SqlCommand com = new SqlCommand(str, con);
            com.ExecuteNonQuery();
            con.Close();
            return true;
        }
        public void xoa(string maloai)
        {
            con.Open();
            string str = "delete from loaivattu where maloai='" + maloai + "'";
            SqlCommand com = new SqlCommand(str, con);
            try
            {
                com.ExecuteNonQuery();
                
            }
            catch (SqlException)
            {
                MessageBox.Show("Lỗi khoá Ngoại");
                con.Close();
               
            }
            con.Close();

        }
        public void sua(CLoaivattu a)
        {
           
            con.Open();
            string str = "update loaivattu set tenloai=@tenloai where maloai=@maloai";
            SqlCommand com = new SqlCommand(str, con);
            com.Parameters.AddWithValue("@maloai", a.Maloai);
            com.Parameters.AddWithValue("@tenloai",a.Tenloai);
            com.ExecuteNonQuery();
            con.Close();
        }
        public List<CLoaivattu> getdsloaivattu()
        {
            List<CLoaivattu> l = new List<CLoaivattu>();
            con.Open();
            string strCom = "select * from loaivattu";
            SqlCommand com = new SqlCommand(strCom, con);
            SqlDataReader data = com.ExecuteReader();
            while (data.Read())
            {
                CLoaivattu vt=new CLoaivattu();
                vt.Maloai=data["maloai"].ToString();
                vt.Tenloai=data["tenloai"].ToString();
                l.Add(vt);

            }
            con.Close();
            return l;
        }
        public CLoaivattu tim(string str)
        {
            con.Open();
            string strCom = "select * from loaivattu where maloai=@str OR tenloai=@str";

            SqlCommand com = new SqlCommand(strCom, con);
            com.Parameters.AddWithValue("@str", str);
            SqlDataReader data = com.ExecuteReader();
            CLoaivattu x = null;
            while (data.Read())
            {
                x = new CLoaivattu();
                x.Maloai = data["maloai"].ToString();
                x.Tenloai = data["tenloai"].ToString();

                break;
            }

            con.Close();
            return x;
        }
    }
}
